/*
 * @FileName: app.js
 * @Describe: 
 * @DateTime: 2021/07/09 16:28:58
 * @Author  : ForretserX
 * @Version : V1.0.0
 * @License : MIT
 */
  
  
const express = require('express');
const path = require('path');
const favicon = require('save-favicon');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');

var index = require('./routes/index');
var data = require('./routes/data');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs'); //指定模板ejs

// uncomment after placing your favicon in /public
// app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extends: false}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public'))); // static res

app.use('/', index)
app.use('/data', data)

// Fetch Error
// Catch 404 and forward to error handler
app.use(function(req, res, next){
    var err = new Error('Not Found 404 !');
    err.status = 404;
    next(err);
});

// error handler
app.use(function(err, req, res, next){
    // set local, only providing error in development
    res.locals.message = err.message;
    res.locals.error = req.app.get('env') === 'development' ? err : {};

    // render the error page
    res.status(err.status || 500);
    res.render('error'); // render: let error.ejs to make page
});

module.exports = app;


